package com.eascs.web.p2p.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.p2p.common.entity.BannerSetEntity;
import com.eascs.p2p.common.service.IBannerService;
import com.eascs.p2p.common.vo.BannerVO;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.p2p.common.BaseMasterViewController;
import com.eascs.web.p2p.utils.StringUtils;

@Controller
@RequestMapping("/banner")
public class BannerController extends BaseMasterViewController {

	@Reference
	IBannerService  bannerService;

	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "admin");
	}
	/**
	 * @Title: index
	 * @Description: banner图列表
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping(value = "/index")
	public String index(BannerVO vo) {

		vo.setPageSize(10);
		ModelResult<PageResult<Object[]>> model = bannerService.findPageBannerSetEntity(vo);
		PageResult<Object[]> pageResult=new PageResult<Object[]>();
		if(model.isSuccess()){
			 pageResult = model.getModel();
		} 
			
		

		String[] titles = { "编号", "名称", "链接URL", "排序" ,"状态"};
		
		AjaxButton openButton=new AjaxButton("启用                   ", "/bannerRest/isDisplay?isDisplay=1&id=");
		openButton.setShowButtonFilter(new ShowButtonFilter(){

			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "暂停".equals(params[5]);
			}
			
		});
		
		AjaxButton closeButton=new AjaxButton("禁用                   ", "/bannerRest/isDisplay?isDisplay=0&id=");
		closeButton.setShowButtonFilter(new ShowButtonFilter(){

			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "启用".equals(params[5]);
			}
			
		});
		
		RedirectButton Button3=new RedirectButton("修改                   ", "/banner/edit_banner?id=");
		AjaxButton showButton=new AjaxButton("预览                   ", "/bannerRest/preview_banner?id=");
		Button[] detailButton = new Button[] {openButton,closeButton,Button3,showButton};
	
		String findCondition = "";
		String pageUrl = "/banner/index";

		if (vo != null) {
			String name = vo.getName() == null ? "" : vo.getName();
			String code = vo.getCode() == null ? "" : vo.getCode();
		

			findCondition = "?name=" + name +"&code=" + code ;

			pageUrl = pageUrl + findCondition;
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl + "&showPage=");
		paginationInfo.setBuildPagingNavigation(true);
		// paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		
		BaseRestResult.refresh();
		return "/banner/index";
	}
	
	@RequestMapping(value = "/add_banner")
	public String addBanner() {
		BannerSetEntity vo=new BannerSetEntity();
		int time=StringUtils.getTime();
		String code="banner-" ;
		vo.setCode(code+time);
		vo.setIsLinkEnabled(0);
		vo.setTarget(1);
		ViewContext.put("vo", vo);
		ViewContext.put("title", "添加");
		return "/banner/banner_edit";
	}
	
	@RequestMapping(value = "/edit_banner")
	public String editBanner(String id) {
		ModelResult<BannerSetEntity> model= bannerService.findBannerSetEntityDetail(id);
		BannerSetEntity vo=new BannerSetEntity();
		if(model.isSuccess()){
			vo=model.getModel();
		}
		ViewContext.put("vo", vo);
		ViewContext.put("title", "修改");
		return "/banner/banner_edit";
	}

	
}
